/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.micromall.common.utils;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class RequestUtil {
	private static transient Log log = LogFactory.getLog(RequestUtil.class);

	public static void setCookie(HttpServletResponse response, String name, String value, String path) {
		if (log.isDebugEnabled()) {
			log.debug("Setting cookie '" + name + "' on path '" + path + "'");
		}

		Cookie cookie = new Cookie(name, value);
		cookie.setSecure(false);
		cookie.setPath(path);
		cookie.setMaxAge(2592000);

		response.addCookie(cookie);
	}

	public static Cookie getCookie(HttpServletRequest request, String name) {
		Cookie[] cookies = request.getCookies();
		Cookie returnCookie = null;

		if (cookies == null) {
			return returnCookie;
		}

		for (int i = 0; i < cookies.length; ++i) {
			Cookie thisCookie = cookies[i];

			if ((!(thisCookie.getName().equals(name))) || (thisCookie.getValue().equals("")))
				continue;
			returnCookie = thisCookie;

			break;
		}

		return returnCookie;
	}

	public static void deleteCookie(HttpServletResponse response, Cookie cookie, String path) {
		if (cookie == null)
			return;
		cookie.setMaxAge(0);
		cookie.setPath(path);
		response.addCookie(cookie);
	}

	public static String getAppURL(HttpServletRequest request) {
		StringBuffer url = new StringBuffer();
		int port = request.getServerPort();
		if (port < 0) {
			port = 80;
		}
		String scheme = request.getScheme();
		url.append(scheme);
		url.append("://");
		url.append(request.getServerName());
		if (((scheme.equals("http")) && (port != 80)) || ((scheme.equals("https")) && (port != 443))) {
			url.append(':');
			url.append(port);
		}
		url.append(request.getContextPath());
		return url.toString();
	}

	public static String getIpAddr(HttpServletRequest request) {
		String ip = request.getHeader("x-forwarded-for");
		if ((ip == null) || (ip.length() == 0) || ("unknown".equalsIgnoreCase(ip))) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if ((ip == null) || (ip.length() == 0) || ("unknown".equalsIgnoreCase(ip))) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if ((ip == null) || (ip.length() == 0) || ("unknown".equalsIgnoreCase(ip))) {
			ip = request.getRemoteAddr();
		}
		ip = replaceSpecialChars(ip);
		if (org.apache.commons.lang.StringUtils.length(ip) > 20) {
			ip = "非法IP";
		}
		return ip;
	}

	public static Long convertIpToNumber(String ip) {
		if (StringUtils.isEmpty(ip))
			return Long.valueOf(0L);
		String[] ipArr = StringUtils.split(ip, ".");
		if (ipArr.length != 4) {
			log.error("非法IP：" + ip);
			return Long.valueOf(0L);
		}
		return Long.valueOf(Long.parseLong(ipArr[0]) * 256L * 256L * 256L + Long.parseLong(ipArr[1]) * 256L * 256L
				+ Long.parseLong(ipArr[2]) * 256L + Long.parseLong(ipArr[3]));
	}

	public static String replaceSpecialChars(String s) {
		if (s == null) {
			return null;
		}
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < s.length(); ++i) {
			char c = s.charAt(i);
			switch (c) {
				case '\'' :
					sb.append("&apos;");
					break;
				case '<' :
					sb.append("&lt;");
					break;
				case '>' :
					sb.append("&gt;");
					break;
				case '"' :
					sb.append("&quot;");
					break;
				default :
					sb.append(c);
			}
		}
		return sb.toString();
	}
}